Exercise 1: Explain the value of the here::here package

“here::here()” provides a simple way to detect the root directory as well as set a working directory. What I like about “here” package most is that I can easily detect root directory of a file or data or work with paths, and those do not need to correspond to the working directory. That is the big difference between ‘hear’ and ‘setwd’ or ‘getwd’. Moreover, by setting relative paths, users do not need to specify all paths explicitly, which makes R project accessible in a platform-independent way.

Exercise 2: Factor management

Step1. Selecting datase: gapminder

Step 2: Check levels I will first check (1) whether continent is factor or character and (2) what are the levels

class(gapminder$continent)
## [1] "factor"
levels(gapminder$continent)
## [1] "Africa"   "Americas" "Asia"     "Europe"   "Oceania"

Step 3: Drop “Oceania”"

gap_drop_oceania <- gapminder %>% 
  filter(!continent == "Oceania")
gap_drop_oceania  %>% 
  droplevels()
## # A tibble: 1,680 x 6
##    country     continent  year lifeExp      pop gdpPercap
##    <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Afghanistan Asia       1952    28.8  8425333      779.
##  2 Afghanistan Asia       1957    30.3  9240934      821.
##  3 Afghanistan Asia       1962    32.0 10267083      853.
##  4 Afghanistan Asia       1967    34.0 11537966      836.
##  5 Afghanistan Asia       1972    36.1 13079460      740.
##  6 Afghanistan Asia       1977    38.4 14880372      786.
##  7 Afghanistan Asia       1982    39.9 12881816      978.
##  8 Afghanistan Asia       1987    40.8 13867957      852.
##  9 Afghanistan Asia       1992    41.7 16317921      649.
## 10 Afghanistan Asia       1997    41.8 22227415      635.
## # … with 1,670 more rows
gap_drop_oceania
## # A tibble: 1,680 x 6
##    country     continent  year lifeExp      pop gdpPercap
##    <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Afghanistan Asia       1952    28.8  8425333      779.
##  2 Afghanistan Asia       1957    30.3  9240934      821.
##  3 Afghanistan Asia       1962    32.0 10267083      853.
##  4 Afghanistan Asia       1967    34.0 11537966      836.
##  5 Afghanistan Asia       1972    36.1 13079460      740.
##  6 Afghanistan Asia       1977    38.4 14880372      786.
##  7 Afghanistan Asia       1982    39.9 12881816      978.
##  8 Afghanistan Asia       1987    40.8 13867957      852.
##  9 Afghanistan Asia       1992    41.7 16317921      649.
## 10 Afghanistan Asia       1997    41.8 22227415      635.
## # … with 1,670 more rows

Step 4: Reorder the levels of country or continent. I will select 4 East Asia countries and reorder the levels of country by gdpPercap.

eastasia <- c("Japan", "Korea, Rep.", "China", "Korea, Dem. Rep.")
east_asia <- gapminder %>%
  filter(country %in% eastasia)

ggplot(east_asia, aes(x=year, y= gdpPercap, 
            color = fct_reorder2(country, year, gdpPercap))) +
  geom_line()+ 
  labs (color="country")

Exercise 3: File input/output (I/O)

Step 1. Export the East_asian file

write_csv(east_asia, here("east_asia.csv"))

Step 2. Import the file again.

read_csv(here::here("east_asia.csv"))

Exercise 4: Visualization design

Step 1: Selecting one previous plot: (hw 03)How is life expectancy changing over time on different continents?

plot1 <- gapminder %>%
  group_by(continent, year) %>%    
  mutate(life_expectancy = mean(lifeExp)) %>%
  ggplot(aes(year, life_expectancy, colour = continent)) + geom_line(alpha = 0.4)

plot1

Step 2: Revising the plot: chaging the labels; adding the title; making it interactive using plotly package

plot2 <- gapminder %>%
  group_by(continent, year) %>%    
  mutate(life_expectancy = mean(lifeExp)) %>%
  ggplot(aes(year, life_expectancy, colour = continent)) +
  geom_line(alpha = 0.4) +
  labs(title="Life expectancy changes by continent", x="Year", y="Life expectancy") +
  theme(panel.grid.minor = element_blank())

plot2 %>%
  ggplotly()

Step 3: Placing these plots side-by-side:

(plot_comparison<- gridExtra::grid.arrange(plot1, plot2, ncol = 2))

## TableGrob (1 x 2) "arrange": 2 grobs
##   z     cells    name           grob
## 1 1 (1-1,1-1) arrange gtable[layout]
## 2 2 (1-1,2-2) arrange gtable[layout]

Exercise 5: Writing figures to file

ggsave("life_expectancy.png", plot = plot_comparison, width = 20, height = 10, dpi = 300)